package cn.hhy.org.controller;

import cn.hhy.org.domain.Employee;
import cn.hhy.org.domain.Employee;
import cn.hhy.org.query.EmployeeQuery;
import cn.hhy.org.service.IEmployeeService;
import cn.hhy.org.service.IEmployeeService;
import cn.hhy.utils.AjaxResult;
import cn.hhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    public List<Employee> loadAll(){
        return employeeService.loadAll();

    }
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            } else {
                employeeService.save(employee);
            }

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败！" + e.getMessage());
        }





    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        try {
            employeeService.delete(id);


            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！" + e.getMessage());
        }

    }
    @GetMapping("/{id}")
    public  Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);

    }
    @PatchMapping
    public AjaxResult PatchDelete(@RequestBody Long[] ids) {

        try {
            employeeService.PatchDelete(ids);

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！" + e.getMessage());
        }

    }
    @PostMapping
    public QueryResult<Employee> employeeLoadAll(@RequestBody EmployeeQuery employeeQuery){

        return employeeService.employeeLoadAll(employeeQuery);



    }


}
